package com.dapeng.jackson.serializer;

import com.dapeng.jackson.abstracts.BaseJKAnnotationSerializer;
import com.dapeng.jackson.abstracts.BaseJKEnumKey;
import com.dapeng.jackson.annotations.JKEnumSerializer;
import com.dapeng.jackson.enums.JKEnumMode;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.util.ReflectionUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongProject study
 * @BelongPackage com.dapeng.jackson.serializer
 * @Description TODO
 * @ClassName JKEnumFormatSerializer
 * @Author xupenghong
 * @Date 2024/9/13 16:16
 * @Version 1.0.0
 */
public class JKEnumFormatSerializer extends BaseJKAnnotationSerializer<JKEnumSerializer, Object> {

    /**
     * 只对枚举类型或者枚举List使用此序列化器
     */
    @Override
    protected boolean isFilterSerializer(BeanProperty property) {
        if (Enum.class.isAssignableFrom(fieldClass)) {
            return true;
        }

        if (List.class.isAssignableFrom(fieldClass)) {
            //  获取泛型参数
            JavaType type = property.getType();
            Class<?> rawClass = type.getContentType().getRawClass();
            if (Enum.class.isAssignableFrom(rawClass)) {
                return true;
            } else {
                return false;
            }

        }
        return false;
    }

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (value == null) {
            return;
        }

        if (value instanceof Enum) {
            //单一枚举
            Object result = getEnumValue((Enum) value);
            gen.writeObject(result);
        } else if (List.class.isAssignableFrom(value.getClass())) {
            // 枚举列表
            List<Enum> valueList = (List<Enum>) value;
            List<Object> result = new ArrayList<>();
            for (Enum tmp : valueList) {
                Object enumValue = getEnumValue(tmp);
                result.add(enumValue);
            }
            gen.writeObject(result);
        }
    }

    private Object getEnumValue(Enum value) {
        Object result = null;
        JKEnumMode mode = annotation.mode();
        if (mode.equals(JKEnumMode.FIELD)) {
            String fieldName = annotation.fieldName();
            // 获取枚举类的字段值
            Field field = ReflectionUtils.findField(value.getClass(), fieldName);
            if (field == null) {
                throw new IllegalArgumentException(value.getClass().getSimpleName() + "枚举类中不存在该字段" + fieldName);
            }
            field.setAccessible(true);
            Object fieldValue = ReflectionUtils.getField(field, value);
            result = fieldValue != null ? fieldValue.toString() : null;
        } else if (mode.equals(JKEnumMode.TO_STRING)) {
            result = value.toString();
        } else if (mode.equals(JKEnumMode.BASE_ENUM_KEY0)) {
            result = ((BaseJKEnumKey) value).getEnumVey0();
        } else if (mode.equals(JKEnumMode.BASE_ENUM_KEY1)) {
            result = ((BaseJKEnumKey) value).getEnumVey1();
        } else if (mode.equals(JKEnumMode.NAME)) {
            result = value.name();
        }
        return result;
    }
}
